package com.example.neusoft215gdh.androidpro_21.Model;
import com.example.neusoft215gdh.androidpro_21.Interface.PeiXunFace;
import com.example.neusoft215gdh.androidpro_21.JavaBean.PeiXunDetail;
import com.example.neusoft215gdh.androidpro_21.JavaBean.PeiXunJiBie;
import com.example.neusoft215gdh.androidpro_21.JavaBean.PeiXunListBean;
import com.example.neusoft215gdh.androidpro_21.Listener.JieBieListener;
import com.example.neusoft215gdh.androidpro_21.Listener.PeiXunDetailListener;
import com.example.neusoft215gdh.androidpro_21.Listener.PeiXunListListener;
import com.example.neusoft215gdh.androidpro_21.Service.PxService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
 * Created by hp-pc on 2017/9/28.
 */

public class PeiXunModel implements PeiXunFace {

    private Retrofit retrofit;
    private PxService pxService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址
    public PeiXunModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 获得培训列表
     * 高德翰
     * @param SessionId 个人id账号
     * @param page 指定是哪一个页面
     * @param listListener 监听对象
     */
    @Override
    public void getList(String SessionId, int page,final PeiXunListListener listListener) {
                pxService = retrofit.create(PxService.class);
        Call<List<PeiXunListBean>> call =pxService.getPeiXunList(SessionId,page);
        call.enqueue(new Callback<List<PeiXunListBean>>() {
            @Override
            public void onResponse(Call<List<PeiXunListBean>> call, Response<List<PeiXunListBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<PeiXunListBean>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }


    /**
     * 培训列表的详情
     *
     * @param sessionId
     * @param id
     * @param listener
     */
    @Override
    public void getPeiXunDetailList(String sessionId, String id,final PeiXunDetailListener listener) {
        pxService = retrofit.create(PxService.class);
        Call<PeiXunDetail> call= pxService.getPeiXunDetail(sessionId,id);
        call.enqueue(new Callback<PeiXunDetail>() {
            @Override
            public void onResponse(Call<PeiXunDetail> call, Response<PeiXunDetail> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<PeiXunDetail> call, Throwable t) {

            }
        });
    }

    @Override
    public void getJieBie(String SessionId, int page,String mod, int degree,final JieBieListener listener) {
        pxService = retrofit.create(PxService.class);
        Call<List<PeiXunJiBie>> call=pxService.peixunList(SessionId,page,mod,degree);
        call.enqueue(new Callback<List<PeiXunJiBie>>() {
            @Override
            public void onResponse(Call<List<PeiXunJiBie>> call, Response<List<PeiXunJiBie>> response) {
                if (response.isSuccessful() && response.body() != null) {
                        listener.onResponseRumen(response.body());
                    } else {
                        listener.onFail("onresponse fail");
                    }
            }

            @Override
            public void onFailure(Call<List<PeiXunJiBie>> call, Throwable t) {

            }
        });
    }




}
